<template>
  <div class="movebox">
    <div class="movego"></div>
    <div class="txt" id="txt">拖动滑块验证</div>
    <div class="move moveBefore" v-move></div>
  </div>
</template>

<script>
export default {
  components: {},
  data() {
    return {};
  },
  mounted() {
    //console.log($('选择器'))
  },
  methods: {},
  directives: {
    move(el) {
      el.onmousedown = function(e) {
        var X = e.clientX - el.offsetLeft;
        document.onmousemove = function(e) {
          var endx = e.clientX - X;
          el.className = "move moveBefore";
          el.style.left = endx + "px";
          // console.log(el.parentNode.children[0])
          var width = $(".movebox").width() - $(".move").width();
          el.parentNode.children[0].style.width = endx + "px";
          el.parentNode.children[1].innerHTML = "拖动滑块验证";
          //临界值小于
          if (endx <= 0) {
            el.style.left = 0 + "px";
            el.parentNode.children[0].style.width = 0 + "px";
            //$('.movego').width(0)
          }
          //临界值大于
          // console.log(el.style.left)
          if (parseInt(el.style.left) >= width) {
            el.style.left = width + "px";
            el.parentNode.children[0].style.width = width + "px";
            el.parentNode.children[1].innerHTML = "验证通过";
            el.className = "move moveSuccess";
            el.onmousedown = null;
          }
        };
      };
      document.onmouseup = function() {
        document.onmousemove = null;
      };
    }
  }
};
</script>

<style lang="less" scoped>
.movebox {
  position: relative;
  background-color: #e8e8e8;
  width: 400px;
  height: 34px;
  line-height: 34px;
  text-align: center;
}
.txt {
  position: absolute;
  top: 0;
  width: 400px;
  -moz-user-select: none;
  -webkit-user-select: none;
  user-select: none;
  -o-user-select: none;
  -ms-user-select: none;
}
.movego {
  background-color: #7ac23c;
  height: 34px;
  width: 0;
}
.move {
  position: absolute;
  top: 0;
  left: 0;
  width: 40px;
  height: 34px;
  border: 1px solid #ccc;
  cursor: move;
}
.moveBefore {
  background: #fff
    url("")
    no-repeat center;
}
.moveSuccess {
  background: #fff
    url("")
    no-repeat center;
}
</style>
